<!-- 教师修改密码 -->

<template>
  <el-card class="box" style="width: 500px;margin: 50px auto">
    <el-form label-width="120px" size="small" :model="form" :rules="rules" ref="pass">
      <el-form-item label="原密码" prop="password">
        <el-input v-model="form.password" autocomplete="off" show-password clearable></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="form.newPassword" autocomplete="off" show-password clearable></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" autocomplete="off" show-password clearable></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button type="primary" @click="save">确定</el-button>
      <el-button type="warning" @click="resetForm">重置</el-button>
    </div>
  </el-card>
</template>

<script>
export default {
  name: "TeaResetPassword",
  data() {
    return {
      form: {},
      Teacher: localStorage.getItem("Teacher") ? JSON.parse(localStorage.getItem("Teacher")) : {},
      rules: {
        password: [
          {required: true, message: '请输入原密码', trigger: 'blur'},
          {min: 1, max: 12, message: '密码不能超过12个字符', trigger: 'blur'}
        ],
        newPassword: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
          {min: 1, max: 12, message: '密码不能超过12个字符', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, message: '请确认新密码', trigger: 'blur'},
          {min: 1, max: 12, message: '密码不能超过12个字符', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.form.username = this.Teacher.username
  },
  methods: {
    save() {
      this.$refs['pass'].validate((valid) => {
        if (valid) {
          if (this.form.newPassword !== this.form.confirmPassword) {
            this.$message.error("两次输入密码不一致！")
            return false;
          }
          this.request.post("/teacher/password", this.form).then(res => {
            console.log(res.code)
            if (res.code === 'code_200') {
              this.$message.success("密码修改成功！")
            }
          })
        }
      });
    },
    resetForm(){
      this.$refs['pass'].resetFields()
    }
  }
}
</script>

<style scoped>
.box {
  padding: 10px;
  border-radius: 10px;
}
</style>